package com.example.wemedia.controller;

import com.example.common.util.R;
import com.example.model.wemedia.dto.LoginDto;
import com.example.model.wemedia.pojo.User;
import com.example.wemedia.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Tag(name = "登录", description = "登录接口")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("/in")
    public R login(@RequestBody LoginDto loginDto){
        Map<String, Object> map = userService.login(loginDto);
        User user = (User) map.get("user");
        String token = (String) map.get("token");
        if (user != null && token != null) {
            map.put("user", user);
            map.put("token", token);
            return R.ok("用户登录成功").data(map);
        } else {
            return R.error("用户登录失败");
        }
    }
}
